package pATT.DataSetsGenerator.dataSetsGenerator.generator.implement;


import java.util.Vector;

import pATT.DataSetsGenerator.dataSetsGenerator.core.implement.Relation;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.IData;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.RandomExponentialData;
import pATT.DataSetsGenerator.dataSetsGenerator.data.implement.RandomPoissonData;

public class RandomPoissonGenerator extends RandomExponentialGenerator {
	
	public RandomPoissonGenerator(Relation relat) {
		super(relat);
	}
	
	@SuppressWarnings("unchecked")
	public Vector generate(IData data){
		if(((RandomPoissonData)data).getMedia() != -1){
		Vector instances = ((RandomPoissonData)data).getAttribute().getValues(); 
		int cantRules = getRelation().getExamples();
		Vector result= new Vector();
		int i = 0; 
		long doub; 
		while(i < cantRules) {
			RandomExponentialData exp = (RandomExponentialData)data;
			double media = exp.getMedia();
			doub = poisson(media);// (0<media<cantidad de instancias-1), doub me da el valor dentro de las instancias 
			if(doub < instances.size()){
				result.addElement(instances.elementAt(((int)doub)));
				i++;
			}
			
		}
		System.out.println(" ");
		return result;
		}
		else{
			System.out.println("------------->FUe al random comun");
			return (new RandomGenerator(relation)).generate(data);
		}
	}
	
	public long poisson(double media){
		/* ================================================== 
		 * Returns a Poisson distributed non-negative integer. 
		 * NOTE: use m > 0
		 * ==================================================
		 */
		double t = 0.0;
		long   x = 0;
		
		while (t < media) {
			t += exponential(media);
			x++;
		}
		return (x - 1);
	}
}
